layui.define(['jquery','form'], function (exports) {
	"use strict";
	var $ = layui.jquery,form=layui.form,object = {
		c: {data:[]}, // 配置项
		f: function (c) { // 数据初始化
			var s=new Date().getTime(),t = this;
			t.c.id = c.id?c.id:'simple_table_choiceshop_detail';
			t.c.tableId = c.tableelem?c.tableelem:'table-'+s;
			//c.data = JSON.parse('[{"accountName":"费玉清","timeTypeName":"00:00~00:59","operatorName":"周杰伦"}]');
			t.c.data = c.data; // 数据
			
			/**自定义的字段，菜单ID**/
			t.c.pcode = c.pcode;
		},
        load:function () { // 加载控件
        	var h = ``
        		+ `<div class="layui-form layui-form-item" lay-filter="` + this.id + `" style="padding-left: 10px;">`
    				+ `<button type="button" class="layui-btn layui-btn-primary btn-add-sys_col_all"><i class="layui-icon layui-icon-addition"></i> 添加商品</button>`
        		+ `</div>`
            	+ `<div class="layui-form layui-form-item" pane>`
            		+ `<div style="margin-left: 10px;" id="`+this.c.tableId+`">`
            			+ this.loadTable()
            		+ `</div>`
        		+ `</div>`
    		+ `</div>`;
            $('#'+this.c.id).html(h);
            this.listenBtn();
            return;
        },
        loadTable:function () { // 加载表格
            return `<table class="layui-table" style="width: 95%;" lay-skin="row" style="margin:0"><thead>`+this.loadTableOfThead()+`</thead><tbody>`+this.loadTableOfTbody()+`</tbody></table>`;
        },
        loadTableOfThead:function () { // 加载表头
            var h = `<thead><tr class="layui-bg-blue">`;
            h += `<th style="width:8%; text-align: center;">商品编码</th>`;
            h += `<th style="width:8%; text-align: center;">属性</th>`;
            h += `<th style="width:8%; text-align: center;">仓库</th>`;
            h += `<th style="width:8%; text-align: center;">箱规尺寸(cm)</th>`;
            h += `<th style="width:6%; text-align: center;">箱规重量(kg)</th>`;
            h += `<th style="width:8%; text-align: center;">建议售价(当地币)</th>`;
            h += `<th style="width:4%; text-align: center;">单价</th>`;
            h += `<th style="width:4%; text-align: center;">单位</th>`;
            h += `<th style="width:4%; text-align: center;">单箱数量</th>`;
            h += `<th style="width:5%; text-align: center;">首次起订箱数</th>`;
            h += `<th style="width:8%; text-align: center;">再次起订箱数</th>`;
            h += `<th style="width:8%; text-align: center;">生产货期(天)</th>`;
            h += `<th style="width:8%; text-align: center;">预计到货时间</th>`;
            h += `<th style="width:8%; text-align: center;">发货数量</th>`;
            h += `<th style="width:8%; text-align: center;">预购数量</th>`;
            h+= `<th style="width:15%; text-align: center;">操作</th></tr></thead>`;
			return h;
        },
        loadTableOfTbody:function () { // 加载表tbody
        	var t = this;
        	var h = `<tbody>`;
            for(var i=0;i<t.c.data.length;i++){
	            h += `<tr>`;
	            h += `<td style="text-align:center; padding: 1px;">` + t.c.data[i].code + `</td>`;
	            h += `<td style="text-align:center; padding: 1px;"><input type="text" class="layui-input" data-index="` + i + `" data-type="attr" lay-filter="_input"  value="` + (t.c.data[i].attr ? t.c.data[i].attr : ``) + `" style="height: 37px;"></td>`;
	            h += `<td style="text-align:center; padding: 1px;"><input type="text" class="layui-input" data-index="` + i + `" data-type="warehouse" lay-filter="_input"  value="` + (t.c.data[i].warehouse ? t.c.data[i].warehouse : ``) + `" style="height: 37px;"></td>`;
//	            h += `<td style="text-align:center; padding: 1px;"><input type="text" class="layui-input" data-index="` + i + `" data-type="size" lay-filter="_input"  value="` + (t.c.data[i].size ? t.c.data[i].size : ``) + `" style="height: 37px;"><i class="layui-icon layui-icon-more" style="position:absolute;top: 10px !important;right: 5px !important; width: 20px;" data-index="` + i + `" lay-filter="_select_gaugemaintenance"></i></td>`;
	            h += `<td style="text-align:center; padding: 1px;">` + (t.c.data[i].size ? t.c.data[i].size : ``) + `<i class="layui-icon layui-icon-more" style="position:absolute;top: 10px !important;right: 5px !important; width: 20px;" data-index="` + i + `" lay-filter="_select_gaugemaintenance"></i></td>`;
	            h += `<td style="text-align:center; padding: 1px;">` + (t.c.data[i].weight ? t.c.data[i].weight : ``) + `</td>`;
	            h += `<td style="text-align:center; padding: 1px;"><input type="number" class="layui-input" data-index="` + i + `" data-type="retailPrice" lay-filter="_input"  value="` + (t.c.data[i].retailPrice ? t.c.data[i].retailPrice : ``) + `" style="height: 37px;"></td>`;
	            h += `<td style="text-align:center; padding: 1px;"><input type="number" class="layui-input" data-index="` + i + `" data-type="originPrice" lay-filter="_input"  value="` + (t.c.data[i].originPrice ? t.c.data[i].originPrice : ``) + `" style="height: 37px;"></td>`;
	            h += `<td style="text-align:center; padding: 1px;"><input type="text" class="layui-input" data-index="` + i + `" data-type="unitName" lay-filter="_input"  value="` + (t.c.data[i].unitName ? t.c.data[i].unitName : ``) + `" style="height: 37px;"></td>`;
	            h += `<td style="text-align:center; padding: 1px;">` + (t.c.data[i].numPerBox ? t.c.data[i].numPerBox : ``) + `</td>`;
	            h += `<td style="text-align:center; padding: 1px;"><input type="number" class="layui-input" data-index="` + i + `" data-type="numBuyMin" lay-filter="_input"  value="` + (t.c.data[i].numBuyMin ? t.c.data[i].numBuyMin : ``) + `" style="height: 37px;"></td>`;
	            h += `<td style="text-align:center; padding: 1px;"><input type="number" class="layui-input" data-index="` + i + `" data-type="numBuyMinSecond" lay-filter="_input"  value="` + (t.c.data[i].numBuyMinSecond ? t.c.data[i].numBuyMinSecond : ``) + `" style="height: 37px;"></td>`;
	            h += `<td style="text-align:center; padding: 1px;"><input type="number" class="layui-input" data-index="` + i + `" data-type="period" lay-filter="_input"  value="` + (t.c.data[i].period) + `" style="height: 37px;"></td>`;
	            h += `<td style="text-align:center; padding: 1px;"><input type="text" class="layui-input" data-index="` + i + `" data-type="timeArrivePredict" lay-filter="_input"  value="` + (t.c.data[i].timeArrivePredict ? t.c.data[i].timeArrivePredict : ``) + `" style="height: 37px;"></td>`;
	            h += `<td style="text-align:center; padding: 1px;"><input type="number" class="layui-input" data-index="` + i + `" data-type="numAll" lay-filter="_input"  value="` + (t.c.data[i].numAll) + `" style="height: 37px;"></td>`;
	            h += `<td style="text-align:center; padding: 1px;"><input type="number" class="layui-input" data-index="` + i + `" data-type="numPreBuy" lay-filter="_input"  value="` + (t.c.data[i].numPreBuy) + `" style="height: 37px;"></td>`;
                h += `<td style="text-align:center; padding: 1px;"><a class="layui-btn layui-btn-danger layui-btn-sm" data-index="` + i + `" lay-filter="remove"><i class="layui-icon layui-icon-delete"></i>移除</a></td>`
                h+= `</tr>`;
            }
            
            h += `</tbody>`;
			return h;
        },
        listenBtn:function () { // 添加监听
            var t = this;
            // 监听“添加商品”按钮
            $('#' + t.c.id).find('.btn-add-sys_col_all').on('click',function (e) {
	            
            	/*
				layer.open({
					type: 2,
					title: "新增商品",
					area: ['90%', '90%'],
					content: '/hr/choiceshop/toAddDetail?pcode=' + t.c.pcode
				});
				*/
				
            	// 选择商品
			    layer.open({
			        type: 2,
			        area: [($(window).width() - 200) + 'px', ($(window).height() - 100) + 'px'],
			        fix: false,//不固定
			        offset: '10%',//距离顶部
			        maxmin: true,//最大最小化
			        shade: 0.3,//遮罩
			        title: '选择商品',
			        content: '/business/productactive/select?deleted=0&vSfJp=是',
			        btn: ['确定', '取消'],
			        shadeClose: true,// 弹层外区域关闭
			        yes: function (index, layero) {
			            var res = window["layui-layer-iframe" + index].callbackdata();
			            var row = {};
			            row['code'] = res.code;
			            row['period'] = 0;
			            row['numAll'] = 0;
			            row['numPreBuy'] = 0;
			            t.addRow(row);
			            layer.close(index);
			        }
			    });
            });
            
			// 监听各个输入框
			$('input[lay-filter="_input"]').on('change',function (e) {
				var index = $(this).attr('data-index');
				var key = $(this).attr('data-type');
				var value = $(this).val();
				t.c.data[index][key] = value;
				return;
			});
            
			// 监听箱规选择
			$('i[lay-filter="_select_gaugemaintenance"]').on('click',function (e) {
				var rowIndex = $(this).attr('data-index');
		    	
		    	var code = t.c.data[rowIndex]['code'];
		    	console.log('code---', code);
		    	if(code == null || code == undefined || code == '') {
		    		layer.msg('请先输入商品编码');
		    		return;
		    	}
		    	
		    	var warehouse = t.c.data[rowIndex]['warehouse'];
		    	if(warehouse == null || warehouse == undefined || warehouse == '') {
		    		layer.msg('请先填写仓库');
		    		return;
		    	}
				
			    layer.open({
			        type: 2,
			        area: [($(window).width() - 200) + 'px', ($(window).height() - 100) + 'px'],
			        fix: false,//不固定
			        offset: '10%',//距离顶部
			        maxmin: true,//最大最小化
			        shade: 0.3,//遮罩
			        title: '箱规维护列表',
			        content: '/storage/gaugeMaintenance/select?code=' + code + '&warehouseAddress=' + warehouse,
			        btn: ['确定', '取消'],
			        shadeClose: true,// 弹层外区域关闭
			        yes: function (index, layero) {
			            var res = window["layui-layer-iframe" + index].callbackdata();
			            t.c.data[rowIndex]['size'] = res.length + '*' + res.width + '*' + res.height;
			            t.c.data[rowIndex]['weight'] = res.weight;
			            t.c.data[rowIndex]['numPerBox'] = res.num;
			            layer.close(index);
						t.reload(t.c);
			        }
			    });
				return;
			});
            
            // 监听“移除”
            $('#' + t.c.tableId).find('a[lay-filter="remove"]').on('click',function (e) {
				var index = $(this).attr('data-index');
				var guid = t.c.data[index]['guid'];
				if(guid != null && guid != undefined && guid != '') {
					$.ajax({
						type: "POST",
						url: '/hr/choiceshop/deleteDetail',
						data: {guid: guid},
						success: function (res) {
							layer.msg(res.msg);
							if (res.code == 0) {
								t.c.data.splice(index, 1);
								t.reload(t.c);
							}
						}
					});
				} else {
					t.c.data.splice(index, 1);
					t.reload(t.c);
				}
			});
            
            // 表单控件重新加载
        	form.render();
        },
        reloadTable:function () { // 重新加载表格
            $('#'+this.c.tableId).html(this.loadTable());
            this.listenBtn();
        },
        getData:function () {
            return this.c.data;
        },
        init:function (c) {this.f(c);this.load();return object;},
        reload:function (c) {c.data = this.c.data;$('#' + this.c.id).empty();return this.init(c);},
        reloadData:function () {
        	var t = this;
			$.ajax({
				type: "POST",
				url: '/hr/choiceshop/detailData',
				data: {pcode: t.c.pcode},
				success: function (res) {
					if (res.code == 0) {
						t.c.data = res.data;
						t.reload(t.c);
					} else {
						layer.msg('加载失败，请刷新页面');
					}
				}
			});
        },
        addRow:function (row) {
        	var c = this.c;
        	c.data.push(row);
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        addRows:function (rows) {
        	var c = this.c;
        	for (var i = 0; i < rows.length; i++) {
	        	c.data.push(rows[i]);
			}
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        editRow:function (index, row) {
        	var c = this.c;
			c.data[index] = row;
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        editRowOfAttr:function (index, row) {
        	var c = this.c;
        	for(var key in row){
        		c.data[index][key] = row[key]
        	}
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        reloadRows:function (rows) {
        	var c = this.c;
        	c.data = rows;
        	return this.reload(c);
        }
	};
	exports('simple_table_choiceshop_detail', object);
});